//  store.js 中都mutation中增加添加和删除token的方法

const state = {
  isLogin: '0',
  ser: null,
  token: localStorage.getItem('token') ? localStorage.getItem('token') : '', // token
  id: localStorage.getItem('id') ? localStorage.getItem('id') : '', // id
  status: localStorage.getItem('status') ? localStorage.getItem('status') : '',
  username: localStorage.getItem('username')
    ? localStorage.getItem('username')
    : '', // username
};
const getters = {
  // 监听数据变化的
  getToken(state) {
    // 获取本地存储的token
    if (!state.token) {
      state.token = JSON.parse(localStorage.getItem(token));
    }
    return state.token;
  },
  getId(state) {
    // 获取本地存储的id
    if (!state.token) {
      state.id = JSON.parse(localStorage.getItem(id));
    }
    return state.id;
  },
  getstatus(state) {
    // 获取本地存储的id
    if (!state.token) {
      state.status = JSON.parse(localStorage.getItem(status));
    }
    return state.id;
  },
  getUsername(state) {
    if (!state.token) {
      state.username = JSON.parse(localStorage.getItem(username));
    }
    return state.username;
  },
};
const mutations = {
  $_setToken(state, value) {
    // 设置存储token
    state.token = value;
    localStorage.setItem('token', value);
  },
  $_removeStorage(state, value) {
    // 删除token
    localStorage.removeItem('token');
  },
  $_setId(state, value) {
    // 设置存储id
    state.id = value;
    localStorage.setItem('id', value);
  },
  $_setUsername(state, value) {
    // 设置存储用户名
    state.username = value;
    localStorage.setItem('username', value);
    // console.log(window.localStorage.username);
  },
  $_setStatus(state, value) {
    // 设置存储用户名
    state.status = value;
    localStorage.setItem('status', value);
  },
};
const actions = {};
export default {
  state,
  mutations,
  actions,
  getters,
};
